'use client';

import React, { useState } from 'react';
import { motion } from 'framer-motion';
import { cn } from '../../utils/cn';
import { Link } from 'umi';

export interface CardHoverItem {
  title: string;
  description: string;
  link: string;
  icon?: React.ReactNode;
  tags?: string[];
}

export interface CardHoverEffectProps {
  items: CardHoverItem[];
  className?: string;
  cardClassName?: string;
  gradientClassName?: string;
  gridClassName?: string;
}

export const CardHoverEffect = ({
  items,
  className,
  cardClassName,
  gradientClassName,
  gridClassName,
}: CardHoverEffectProps) => {
  let [hoveredIndex, setHoveredIndex] = useState<number | null>(null);

  return (
    <div
      className={cn(
        'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 py-10',
        gridClassName,
        className,
      )}
    >
      {items.map((item, idx) => (
        <div
          key={item.link}
          className="relative group block p-2 h-full w-full"
          onMouseEnter={() => setHoveredIndex(idx)}
          onMouseLeave={() => setHoveredIndex(null)}
        >
          <Link to={item.link}>
            <div
              className={cn(
                'relative overflow-hidden h-full w-full p-4 rounded-xl transition duration-300',
                'bg-white dark:bg-gray-800 group-hover:shadow-xl',
                cardClassName,
              )}
            >
              <div className="relative z-10">
                <div className="p-4">
                  {item.icon && <div className="mb-2 text-primary">{item.icon}</div>}
                  <h4 className="text-xl font-bold tracking-tight dark:text-white">{item.title}</h4>
                  <p className="mt-2 text-gray-500 dark:text-gray-300">{item.description}</p>
                  {item.tags && item.tags.length > 0 && (
                    <div className="flex flex-wrap gap-2 mt-3">
                      {item.tags.map((tag, tagIdx) => (
                        <span
                          key={tagIdx}
                          className="px-2 py-1 text-xs rounded-full bg-blue-50 text-blue-600 dark:bg-blue-900/30 dark:text-blue-300"
                        >
                          {tag}
                        </span>
                      ))}
                    </div>
                  )}
                </div>
              </div>
              {hoveredIndex === idx && (
                <motion.div
                  className={cn(
                    'absolute inset-0 bg-gradient-to-br from-blue-50 via-blue-100 to-blue-50 opacity-60 z-0',
                    'dark:from-blue-900/20 dark:via-blue-900/10 dark:to-blue-900/20',
                    gradientClassName,
                  )}
                  layoutId="hoverBackground"
                  initial={{ opacity: 0 }}
                  animate={{ opacity: 0.6 }}
                  exit={{ opacity: 0 }}
                  transition={{ type: 'spring', bounce: 0.2, duration: 0.6 }}
                />
              )}
            </div>
          </Link>
        </div>
      ))}
    </div>
  );
};
